﻿using System;
using System.Collections.Generic;

namespace HYRevitCode.FaceCalculateUtility
{
	internal class FaceLinkeHanlder
	{
		internal FaceLinkeHanlder(List<PlanarFacePacker> lstInputFace, bool IfIsAsync)
		{
			this.m_lstFace = lstInputFace;
			this.m_useDelegate = new LinkFaceBackGroudWorkDel(this.LinkFaceAndUpdateShape);
			this.m_ifIsAsync = IfIsAsync;
		}

		internal void DoWork()
		{
			if (this.m_ifIsAsync)
			{
				this.m_AsyncResult = this.m_useDelegate.BeginInvoke(null, null);
				return;
			}
			this.m_useDelegate();
		}

		internal bool IfComplete()
		{
			return !this.m_ifIsAsync || this.m_AsyncResult.IsCompleted;
		}

		private void LinkFaceAndUpdateShape()
		{
			for (int i = 0; i < this.m_lstFace.Count; i++)
			{
				for (int j = i + 1; j < this.m_lstFace.Count; j++)
				{
					this.m_lstFace[i].TryAddLinkePacker(this.m_lstFace[j]);
				}
			}
			foreach (PlanarFacePacker planarFacePacker in this.m_lstFace)
			{
				planarFacePacker.UpdataShape();
			}
		}

		private List<PlanarFacePacker> m_lstFace = new List<PlanarFacePacker>();

		private LinkFaceBackGroudWorkDel m_useDelegate;

		private bool m_ifIsAsync;

		private IAsyncResult m_AsyncResult;
	}
}
